﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xiaov.SmartWeb.Infrastructure.ExtMethod;
using Xiaov.SmartWeb.Model.ActiveRecord;
using Xiaov.SmartWeb.Infrastructure.Common;
using Xiaov.SmartWeb.Model;

namespace Xiaov.SmartWeb.Application.PluginSrc.GuestBook
{
    public partial class NewSubJect : BasePage
    {
        private string Id { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            PM_FilterByProperty(ModulePropoty.MODULEURL, "/PluginSrc/GuestBook/SubjectList.aspx");
            if (!Permissions[0].Allow)
            {
                General.ShowAlert("没有授权，请联系管理员！");
            }
            Id = Request.QueryString["id"].ToRequestString();
            if (!IsPostBack)
            {                
                if(!string.IsNullOrEmpty(Id))
                BindSubject();
            }
        }

        private void BindSubject()
        {
            A_GUESTBOOK_SUBJECT model = A_GUESTBOOK_SUBJECT.TryFind(Id);
            if (model != null)
            {
                t_subject.Text = model.TITLE;
                t_content.Text = model.CONTENT;
                t_class.SelectedValue = model.TCLASS;
                t_top.Checked = (model.ISTOP == "1");
            }
            else
            {
                General.ShowAlert("错误的参数！");
            }
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Id))
            {
                A_GUESTBOOK_SUBJECT model = A_GUESTBOOK_SUBJECT.TryFind(Id);
                if (model != null)
                {
                    model.TITLE = t_subject.Text.Trim();
                    model.BOARDID = t_class.SelectedValue;
                    model.TCLASS = t_class.SelectedItem.Text;
                    model.CONTENT = Request.Form["ctl00$ContentPlaceHolder1$t_content"].ToRequestString();
                    model.AUTHOR = PUser.USERNAME;
                    model.PUBDATE = DateTime.Now;
                    model.ISTOP = t_top.Checked?"1":"0";
                    model.Update();
                    model.Refresh();
                    Response.Redirect("ViewSubject.aspx?id="+model.ID);
                }
                else
                {
                    General.ShowAlert("无效的参数");
                }
            }
            else
            {
                A_GUESTBOOK_SUBJECT model = new A_GUESTBOOK_SUBJECT();
                model.ID = Guid.NewGuid().ToString();
                model.TITLE = t_subject.Text.Trim();
                model.BOARDID = t_class.SelectedValue;
                model.TCLASS = t_class.SelectedItem.Text;
                model.CONTENT = Request.Form["ctl00$ContentPlaceHolder1$t_content"].ToRequestString();
                model.AUTHOR = PUser.USERNAME;
                model.PUBDATE = DateTime.Now;
                model.ISLOCK = "0";
                model.ISTOP = t_top.Checked?"1":"0";
                model.Save();
                model.Refresh();
                Response.Redirect("ViewSubject.aspx?id=" + model.ID,true);
            }
        }
    }
}